package org.BUPT.application.service;

import org.BUPT.domain.model.message.AnswerCommented;
import org.BUPT.domain.model.message.CommentCommented;
import org.BUPT.domain.model.question.AnswerBO;
import org.BUPT.domain.model.question.CommentBO;
import org.BUPT.domain.service.impl.CommentServiceImpl;
import org.BUPT.infrastructure.exception.NullObjectException;
import org.BUPT.infrastructure.exception.PersistException;
import org.BUPT.infrastructure.persistent.po.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CommentService {
    @Autowired
    org.BUPT.domain.service.CommentService commentService;
    @Autowired
    AnswerService answerService;
    @Autowired
    MessageService messageService;
    @Autowired
    QuestionService questionService;


    public CommentBO getComment(int id) throws NullObjectException, PersistException {
        Comment comment = commentService.getComment(id);
        if (comment!=null&&comment.getIsDel()!=1){
            return new CommentBO(comment);
        }
        else{
            throw new NullObjectException("所查询的评论不存在");
        }
    }


    public void createComment(int userId,int answerId,int parentId, String content) throws PersistException, NullObjectException {
        AnswerBO answerBO = answerService.getAnswer(answerId);
        if (parentId==-1){
            messageService.addMessage(new AnswerCommented(
                    answerBO.getQuestionId(),
                    userId,
                    answerBO.getUserId()
            ));
        }
        else {
            CommentBO commentBO = new CommentBO(commentService.getComment(parentId));
            messageService.addMessage(new CommentCommented(
                    answerBO.getQuestionId(),
                    userId,
                    commentBO.getUserId()
            ));
        }




        new CommentBO()
                .setAnswerId(answerId)
                .setParentId(parentId)
                .setContent(content)
                .setUserId(userId)
                .setCreateTime(new Date())
                .save();
    }

    public void modifyComment(int id, String content) throws NullObjectException, PersistException {
        getComment(id)
                .setContent(content)
                .setEditTime(new Date())
                .save();
    }
}
